package com.jxdinfo.hussar.example.common.schedulingtask;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.WmWarehouseMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.WmWarehouse;
import com.jxdinfo.hussar.example.common.schedulingtask.service.WmWarehouseService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.WmWarehouseVO;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author QiaoBin
 * @Date 2024/4/02 15:49
 * @Description  md_client仓库表的定时任务
 */
@Component
@AllArgsConstructor
public class  WmHouseScheduleTask {
    private static final Logger logger = LoggerFactory.getLogger(WmHouseScheduleTask.class);

    private WmWarehouseMapper wmWarehouseMapper;

    private WmWarehouseService wmWarehouseService;



//    @Scheduled(cron = "*/5 * * * * ?")
    @Scheduled(cron = "0 30 3 * * ?") // 每天凌晨3点30执行
    public void wmWarehouseTask() {
        logger.info("\n用友-仓库表-定时任务-开始执行");
        LocalDateTime nowTime = LocalDateTime.now();
        synchronized ("wmWarehouseTask"){
            logger.info("用友-仓库表-开始查询仓库表中最大的同步时间");
            LocalDateTime scheduleDate = wmWarehouseMapper.selectMaxScheduleDate();
            String scheduleDateString = "";
            if (scheduleDate != null) {
                scheduleDateString = scheduleDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
            //查询创建时间大于定时任务时间，查询出来后把所有数据全部插进去
            logger.info("\n用友-仓库表-开始查询wm_warehouse表中创建时间大于任务时间的数据,查询参数scheduleDateString={}", scheduleDateString);
            List<WmWarehouseVO> moreWarehouseList = wmWarehouseMapper.selectMoreSchedule(scheduleDateString);
            logger.info("\n用友-仓库表-结束查询wm_warehouse表中创建时间大于任务时间的数据,查询结果moreWarehouseList={}，size={}", moreWarehouseList,moreWarehouseList.size());
            if (moreWarehouseList.size() > 0) {
                ArrayList<WmWarehouse> wmWarehouseList = new ArrayList<>();
                for (int i = 0; i < moreWarehouseList.size(); i++) {
                    logger.info("\n用友-仓库表-当前循环次数i={}", i);
                    WmWarehouseVO wmWarehouseVO = moreWarehouseList.get(i);
                    WmWarehouse wmWarehouse = new WmWarehouse();
                    wmWarehouse.setWarehouseId(wmWarehouseVO.getWarehouseId());
                    wmWarehouse.setWarehouseCode(wmWarehouseVO.getWarehouseCode());     // 编码
                    wmWarehouse.setWarehouseName(wmWarehouseVO.getWarehouseName());     // 名称
                    wmWarehouse.setEnableFlag(wmWarehouseVO.getEnableFlag());     // 是否启用
                    wmWarehouse.setScheduleTime(nowTime);
                    wmWarehouse.setCreateBy(wmWarehouseVO.getCreateBy());
                    wmWarehouse.setCreateTime(wmWarehouseVO.getCreateTime());
                    wmWarehouse.setUpdateBy(wmWarehouseVO.getUpdateBy());
                    wmWarehouse.setUpdateTime(wmWarehouseVO.getUpdateTime());
                    wmWarehouse.setErpWarehouseId(wmWarehouseVO.getWarehouseId());
                    logger.info("\n用友-仓库表-开始向list中存入数据，数据wmWarehouse={},i={}", wmWarehouse,i);
                    wmWarehouseList.add(wmWarehouse);
                }
                logger.info("\n用友-仓库表-开始向wm_warehouse表中插入数据，数据mdVendorList={}", wmWarehouseList);
                boolean b = wmWarehouseService.saveBatch(wmWarehouseList, 100);
                logger.info("\n用友-仓库表-结束向wm_warehouse表中插入数据，插入结果b={}", b);
            }
            if (scheduleDate == null) {
                logger.info("\n用友-仓库表-查询不到任务时间，说明一次也没有同步，之间原封不动的插入就行,也就不需要后续更新");
                return;
            }
            if (scheduleDate != null) {
                logger.info("\n用友-仓库表-开始查询wm_warehouse表中更新时间大于任务时间，查询参数scheduleDateString={}", scheduleDateString);
                List<WmWarehouseVO> moreUpdatehouseList = wmWarehouseMapper.selectUpdateMoreSchedule(scheduleDateString);
                logger.info("\n用友-仓库表-结束查询wm_warehouse表中更新时间大于任务时间,查询结果moreUpdatehouseList={},数量size={}", moreUpdatehouseList, moreUpdatehouseList.size());

                if (moreUpdatehouseList == null || moreUpdatehouseList.size() == 0) {
                    logger.info("\n用友-仓库表-没有查询到更新时间大于任务时间的wm_warehouse表中数据");
                    return;
                }
                // 转成map集合
                logger.info("\n用友-仓库表-开始把用友表中更新时间大于任务时间的数据转换成主键、对象的map集合");
                Map<Long, WmWarehouseVO> wmWarehouseUpdateMap = moreUpdatehouseList.stream().collect(Collectors.toMap(WmWarehouseVO::getWarehouseId, t -> t));
                logger.info("\n用友-仓库表-开始获取用友表中更新时间大于任务时间的数据的主键");
                List<Long> houseIdList = moreUpdatehouseList.stream().map(WmWarehouseVO::getWarehouseId).collect(Collectors.toList());
                logger.info("\n用友-仓库表-wm_warehouse表更新时间大于任务时间的主键集合houseIdList={},size={}", houseIdList, houseIdList.size());

                logger.info("\n用友-仓库表-开始查询wm_warehouse中erp_house_id=erp表中主键的数据,参数houseIdList={}", houseIdList);
                QueryWrapper<WmWarehouse> wmWarehouseWrapper = new QueryWrapper<>();
                wmWarehouseWrapper.in("warehouse_id",houseIdList);
                wmWarehouseWrapper.eq("deleted_flag",0);
                List<WmWarehouse> wmHouseUpdateList = wmWarehouseMapper.selectList(wmWarehouseWrapper);
                logger.info("\n用友-仓库表-结束查询wm_warehouse中erp_house_id=erp表中主键的数据,结果wmHouseUpdateList={},size={}", wmHouseUpdateList, wmHouseUpdateList.size());
                if (wmHouseUpdateList.size() > 0) {
                    ArrayList<WmWarehouse> updateWmHouseList = new ArrayList<>();
                    for (int i = 0; i < moreUpdatehouseList.size(); i++) {
                        logger.info("\n用友-仓库表-当前更新时间大于任务时间，wm_warehouse表更新，次数i={}", i);
                        WmWarehouse wmWarehouse = wmHouseUpdateList.get(i);
                        Long currentMasterId = wmWarehouse.getErpWarehouseId();
                        WmWarehouseVO wmWarehouseVO = wmWarehouseUpdateMap.get(currentMasterId);
                        wmWarehouse.setWarehouseCode(wmWarehouseVO.getWarehouseCode());              // 编码
                        wmWarehouse.setWarehouseName(wmWarehouseVO.getWarehouseName());              // 名称
                        wmWarehouse.setEnableFlag(wmWarehouseVO.getEnableFlag());              // 生效状态

                        wmWarehouse.setScheduleTime(nowTime);
                        wmWarehouse.setCreateBy(wmWarehouseVO.getCreateBy());
                        wmWarehouse.setCreateTime(wmWarehouseVO.getCreateTime());
                        wmWarehouse.setUpdateBy(wmWarehouseVO.getUpdateBy());
                        wmWarehouse.setUpdateTime(wmWarehouseVO.getUpdateTime());
                        logger.info("\n用友-仓库表-向updateWmHouseList存储待要更新的仓库表信息,wmWarehouse={}", wmWarehouse);
                        updateWmHouseList.add(wmWarehouse);
                    }
                    logger.info("\n用友-仓库表-开始更新wm_warehouse数据,列表updateWmHouseList={}", updateWmHouseList);
                    boolean u = wmWarehouseService.updateBatchById(updateWmHouseList);
                    logger.info("\n用友-仓库表-结束更wm_warehouse数据,更新结果u={}", u);
                }
            }
        }
        logger.info("\n用友-仓库表-定时任务-结束执行");
    }
}
